﻿var index = -1;
var presstime = new Date().getTime();

function brandListLiHide() {
    $("#brand-list-li").hide();
}
function processAjaxRequest() {
    //数据清零
    document.getElementById("h_entityid").value = "0";

    var txt_query = $("#query").val();
    if (txt_query == "") {
        brandListLiHide();
        return;
    }
    //获取配置
    //文件目录深度
    var dpt = document.getElementById("c_FilePathDepth").value;
    //文件路径 不含深度
    var filepath = document.getElementById("c_FilePath").value;
    var maxcount = document.getElementById("c_MaxItemCount").value;
    //自定义查询内容
    var customizeQuery = document.getElementById("c_customizeQuery").value;

    if (new Date().getTime() - presstime < 50) return;
    presstime = new Date().getTime();
    $.ajax({
        type: "Post",
        url: dpt + filepath,
        data: "queryword=" + txt_query + "&c=" + maxcount + customizeQuery,

        success: function (msg) {
            if (msg != "") {
                msg = eval(msg);

                var html = "";
                for (var i = 0; i < maxcount; i++) {
                    if (!msg[i]) {
                        continue;
                    }
                    html += "<li id='" + i + "' entityid='" + msg[i].entityid + "' onmouseover='overChangeC(" + i + ")' onmouseout='outChangeC()' title='" + msg[i].entityvalue + "' onClick=setInputVal(" + i + ",0,'" + msg[i].entityid + "')>" + msg[i].entityvalue + "</li>";
                }
                document.getElementById("brand-list-li").innerHTML = html;
                $("#brand-list-li").show();
            }
        }
    });
}

function processKeyup() {
    var keyCode = event.keyCode;
    if (keyCode == 38 || keyCode == 40) {
        return;
    } else if (keyCode == 13) {
        processEnter();
        index = -1;
    } else {
        //隐藏
        if (document.activeElement.id != "brand-list-li")
            brandListLiHide();
        processAjaxRequest();
        index = -1;
    }
}

function processKeydown() {
    var keyCode = event.keyCode;
    if (keyCode == 38 || keyCode == 40) processKeyUpAndDown(keyCode);
}

function processKeyUpAndDown(keyCode) {
    var brand = $('#brand-list-li').children();
    var length = brand.length;
    if (length <= 0) return;
    if (index != -1) outChangeC();

    if (keyCode == 38)
        index <= 0 ? index = length - 1 : index--;
    else
        index >= length - 1 ? index = 0 : index++;

    overChangeC(index);

    setInputVal(index, 1, 0);
}


function processEnter() {
    if (index != -1) setInputVal(index, 0, 0);
}

function AppendMsg(msg) {
    var l = document.getElementById("selectorlblmsg");
    //隐藏信息
    l.style.display = "";
    l.innerText = l.innerText + msg;
}

function printMsg(msg) {
    var l = document.getElementById("selectorlblmsg");
    //隐藏信息
    l.style.display = "";
    l.innerText = msg;
}

function clearMsg() {
    var l = document.getElementById("selectorlblmsg");
    //隐藏信息
    l.style.display = "none";
    l.innerText = "";
}

function setInputVal(index, o, entityid) {
    var v = $('#brand-list-li').children().eq(index).html();
    $('#query').attr("value", v);
    if (entityid != 0 && entityid != "" && entityid != null)
        $('#h_entityid').attr("value", entityid);
    else {
        $('#h_entityid').attr("value", $('#brand-list-li').children().eq(index).attr("entityid"));
    }
    //隐藏信息
    clearMsg();
    if (o == 0) {
        brandListLiHide();
    }
}

function overChangeC(i) {
    outChangeC();
    var brand = $('#brand-list-li').children();
    brand.eq(i).css("background", "#eceff1");
}

function outChangeC() {
    $("#brand-list-li li").css("background", "#fff");
}

$(".brand-list .iSselected").click(function () {
    var input = $(this).children("input");
    var type1 = input.attr("type1");
    if (input.attr("checked") == "checked") {
        input.attr("checked", false);
    } else {
        $("input[" + type1 + "=" + type1 + "]").attr("checked", false);
        input.attr("checked", true);
    }
    $(".brand-list .iSselected").each(function () {
        if ($(this).children("input").attr("checked") == "checked") {
            $(this).addClass("active");
        } else {
            $(this).removeClass("active");
        }
    })
    PostHtml();
});


function showAllBrand() {
    $("#brand a").show();
}
function showBrand() {
    var showTopNum = 5;//设置品牌展示该数量商品后开始展示更多按钮
    var brand = $("#brand a");
    var length = brand.length;
    if (length <= showTopNum) {
        $("#brand .more").hide();
    } else {
        brand.each(function (i) {
            if (i < showTopNum)
                $("#brand a").eq(i).show();
            else
                $("#brand a").eq(i).hide();
        })
        $("a[md='showAll']").show();
    }
}
//showBrand();
$(".brand-list .more").click(function () {
    $(this).hide();
    $(this).siblings(".more").show();
    var md = $(this).attr("md");
    if (md == "showAll") showAllBrand();
    if (md == "show") showBrand();
});